package com.mostromas.ess.project.api;

import com.mostromas.ess.entities.ProjectCardNumber;
import com.mostromas.ess.entities.UserProject;
import com.mostromas.ess.feign.IFeignSystemController;
import com.mostromas.ess.project.req.ProjectCardNumberREQ;
import com.mostromas.ess.project.req.UpdateVersionREQ;
import com.mostromas.ess.project.req.UserProjectsREQ;
import com.mostromas.ess.project.service.IProjectCardNumberService;
import com.mostromas.ess.project.service.IUpdateVersionService;
import com.mostromas.ess.project.service.IUserProjectsService;
import com.mostromas.ess.util.base.Result;
import com.mostromas.ess.util.tools.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Api(value = "软件更新信息接口", description = "查询软件更新信息，不用通过身份认证即可调用接口")
@RestController
@RequestMapping("/api/update")
public class ApiUpdateVersionController {

    @Autowired
    private IUpdateVersionService updateVersionService;
    @Autowired
    private IProjectCardNumberService projectCardNumberService;
    @Autowired
    private IUserProjectsService userProjectsService;
    @Autowired
    private IFeignSystemController systemController;

    @ApiOperation("根据版本号和客户端类型查询是否需要更新")
    @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "软件名称",required = true),@ApiImplicitParam(name = "version",value = "版本信息",required = true)})
    @GetMapping("/getUpdateValidate/{name}/{clientType}/{version}")
    public Result checkVersionIsForcibly(@PathVariable("name") String name, @PathVariable("clientType") String clientType, @PathVariable("version") String version) throws Exception {
        UpdateVersionREQ req = new UpdateVersionREQ();
        req.setName(name);
        req.setVersion(version);
        req.setClientType(Integer.parseInt(clientType));
        return updateVersionService.checkIsForcibly(req);
    }

    @ApiOperation("根据版本号获取需要更新的版本地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "软件名称",required = true),@ApiImplicitParam(name = "clientType",value = "客户端类型",required = true),@ApiImplicitParam(name = "version",value = "版本信息",required = true)})
    @GetMapping("/getUpdateVersions/{name}/{clientType}/{version}")
    public Result getUpdateVersions(@PathVariable("name") String name,@PathVariable("clientType") String clientType,@PathVariable("version") String version) throws Exception {
        UpdateVersionREQ req = new UpdateVersionREQ();
        req.setName(name);
        req.setVersion(version);
        req.setClientType(Integer.parseInt(clientType));
        return updateVersionService.queryListByVersion(req);
    }
    @ApiOperation("根据用户名 卡密激活软件")
    @PostMapping("/activation")
    public Result activationProject(@RequestBody ProjectCardNumberREQ req){
        //查询卡密是否正确和是否已使用
        ProjectCardNumber pcn = projectCardNumberService.checkCardNumberByProjectId(req);
        if(pcn == null){
            System.out.println("卡密错误");
            return Result.error("卡密错误或已使用");
        }
        //查询用户名是否存在
        Result result = systemController.checkUsername(req.getUsername());
        if(!Boolean.parseBoolean(result.getData().toString())){
            System.out.println("用户名错误");
            return Result.error("用户名不正确");
        }
        //卡密正确则给该用户添加使用天数
        UserProject up = userProjectsService.getInfoByUsernameProjectId(req.getUsername(),req.getProjectId());
        if(up == null){
            up = new UserProject();
            up.setProjectId(req.getProjectId());
            up.setUsername(req.getUsername());
            up.setEffectiveTime(new Date());
            up.setHardwareCoding(req.getHardwareCoding());
        }
        up.setDays(pcn.getDays());
        userProjectsService.updateEffectiveTime(up);
        System.out.println("增加使用天数");
        //更新卡密状态
        pcn.setState(true);
        pcn.setUseTime(new Date());
        pcn.setUsername(req.getUsername());
        projectCardNumberService.updateById(pcn);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("更新卡密状态");
        return Result.ok("激活成功，有效期至："+sdf.format(up.getEffectiveTime()),null);
    }

}